package com.yanshibo.house01_web.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.yanshibo.house01_bean.bean.Hourse;
import com.yanshibo.house01_bean.bean.Lei;
import com.yanshibo.house01_bean.bean.PeiTao;
import com.yanshibo.house01_bean.bean.Tem;
import com.yanshibo.house01_service.service.MyService;

//表现层
@Controller
public class MyController {

	@Autowired
	private MyService ser;

	// 列表
	@RequestMapping("list")
	public String list(Model model,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "3") Integer pageSize, Tem tem) {

		PageInfo<Hourse> info = ser.list(pageNum, pageSize, tem);

		model.addAttribute("info", info);
		model.addAttribute("tem", tem);

		return "list";
	}

	// 出租
	@RequestMapping("chu")
	public String chu(String id) {

		ser.chu(id);

		return "redirect:list";
	}

	// 退租
	@RequestMapping("tui")
	public String tui(String id) {

		ser.tui(id);

		return "redirect:list";
	}

	// 删除
	@RequestMapping("del")
	public String del(String id) {

		ser.del(id);

		return "redirect:list";
	}

	// 去添加房屋
	@RequestMapping("toAdd")
	public String toAdd(Model model) {
		List<Lei> leis = ser.listLei();
		model.addAttribute("leis", leis);
		model.addAttribute("hou", new Hourse());
		return "add";
	}

	// 添加房屋
	@RequestMapping("add")
	public String add(@Valid @ModelAttribute("hou") Hourse hou,
			BindingResult res, MultipartFile myfile, Model model)
			throws IllegalStateException, IOException {

		if (res.hasErrors()) {
			List<Lei> leis = ser.listLei();
			model.addAttribute("leis", leis);
			return "add";
		}

		if (myfile != null && !myfile.isEmpty()) {
			String filename = myfile.getOriginalFilename();
			filename = UUID.randomUUID().toString() + filename;
			File file = new File("D:/images", filename);
			myfile.transferTo(file);
			hou.setPicture("/images/" + filename);
		}

		ser.addHou(hou);

		return "redirect:list";
	}

	// 去添加设施
	@RequestMapping("toAddPei")
	public String toAddPei(Integer id, Model model) {
		Hourse hou = ser.listHou(id);
		model.addAttribute("hou", hou);
		model.addAttribute("pei", new PeiTao());
		return "addPei";
	}

	// 添加设施
	@RequestMapping("addPei")
	public String addPei(@Valid @ModelAttribute("pei") PeiTao pei,
			BindingResult res, Model model) {

		PeiTao tao = ser.seletePei(pei);
		if (pei.getName() != null && !"".equals(pei.getName())) {

			if (tao == null) {
				res.rejectValue("id", null, "无此配套设施");
			}
		}
		if (res.hasErrors()) {
			Hourse hou = ser.listHou(pei.getHid());
			model.addAttribute("hou", hou);
			return "addPei";
		}

		pei.setId(tao.getId());

		ser.addPei(pei);

		return "redirect:list";
	}

}
